﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmDonDatHangNVL : Form
    {
        public frmDonDatHangNVL()
        {
            InitializeComponent();
        }

        private void frmLapDonDatHang_Load(object sender, EventArgs e)
        {
            HienThiDanhSach();
        }
        public delegate void LayMa(DonDH_NguyenVatLieuDTO hs);
        public event LayMa KhiLayMa = null;

        DonDH_NguyenVatLieuDTO LayThongTin()
        {

            int dong = dgvDanhSach.SelectedCells.Count;
            dong = dgvDanhSach.SelectedCells[0].RowIndex;
            int ma = int.Parse(dgvDanhSach.Rows[dong].Cells[0].Value.ToString());
            DonDH_NguyenVatLieuDTO ddh = new DonDH_NguyenVatLieuDTO();
            ddh.MaDDH = ma;
            return ddh;
        }

        public void HienThiDanhSach()
        {
            DonDH_NguyenVatLieuBUS ddhBUS = new DonDH_NguyenVatLieuBUS();
            List<DonDH_NguyenVatLieuDTO> listDDH = new List<DonDH_NguyenVatLieuDTO>();
            DonDH_NguyenVatLieuDTO ddhDTO = new DonDH_NguyenVatLieuDTO();
            try
            {
                listDDH = ddhBUS.LayDanhSachDonDH_NguyenVatLieu();
                this.dgvDanhSach.Rows.Clear();
                for (int i = 0; i < listDDH.Count; i++)
                {
                    ddhDTO = listDDH[i];
                    String MaDDH = ddhDTO.MaDDH.ToString();
                    String NgayLap = ddhDTO.NgayLap.ToString();
                    String DiaDiemGiaoHang = ddhDTO.DiaDiemGiaoHang.ToString();
                    String ThoiGianGiaohang = ddhDTO.ThoiGianGiaohang.ToString();
                    String MaNCC = ddhDTO.MaNCC.ToString();
                    String Ten = ddhDTO.Ten.ToString();
                    dgvDanhSach.Rows.Add(MaDDH, NgayLap, DiaDiemGiaoHang, ThoiGianGiaohang, MaNCC, Ten);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.ToString());
            }
        }

        public void HienThiChiTiet(int ma)
        {
            CT_DonDH_NguyenVatLieuBUS ctddhBUS = new CT_DonDH_NguyenVatLieuBUS();
            List<CT_DonDH_NguyenVatLieuDTO> listCTDDH = new List<CT_DonDH_NguyenVatLieuDTO>();
            try
            {
                listCTDDH = ctddhBUS.LayDanhSachDonDH_NguyenVatLieuTheoMa(ma);
                this.dgvChiTiet.Rows.Clear();

                for (int i = 0; i < listCTDDH.Count; i++)
                {
                    CT_DonDH_NguyenVatLieuDTO ctddhDTO = new CT_DonDH_NguyenVatLieuDTO();
                    ctddhDTO = listCTDDH[i];

                    String MaNVL = ctddhDTO.MaNVL.ToString();
                    String Ten = ctddhDTO.Ten.ToString();
                    String SoLuong = ctddhDTO.SoLuong.ToString();
                    String DonViTinh = ctddhDTO.DonViTinh.ToString();
                    String DonGiaBan = ctddhDTO.DonGiaBan.ToString();

                    dgvChiTiet.Rows.Add(MaNVL, Ten, SoLuong, DonViTinh, DonGiaBan);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi dữ liệu");
                MessageBox.Show(ex.ToString());
            }
        }

        private void dgvDanhSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            int intIndex = e.RowIndex;
            dgvDanhSach.Rows[intIndex].Selected = true;
            //Load Chi Tiết
            int dong = dgvDanhSach.SelectedCells.Count;
            if (dong == 0)
                return;
            dong = dgvDanhSach.SelectedCells[0].RowIndex;
            try
            {
                int ma = int.Parse(dgvDanhSach.Rows[dong].Cells[0].Value.ToString());
                HienThiChiTiet(ma);
            }
            catch
            {
                return;
            }
            DonDH_NguyenVatLieuDTO ddh = LayThongTin();
            if (KhiLayMa != null)
            {
                KhiLayMa(ddh);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmThemDonDatHangNVL frm = new frmThemDonDatHangNVL();
            frm.ShowDialog();
        }

        private void btnQuayRa_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}